import { CodeListService } from '../app/codelist-service';
import { EntityBaseService, IContext, HttpResponse } from 'ibiz-core';
import { ICUSTOMER, CUSTOMER } from '../../entities';
import keys from '../../entities/customer/customer-keys';
import { CUSTOMERDTOHelp } from '../../entities/customer/customerdto-help';


/**
 * 客户服务对象基类
 *
 * @export
 * @class CUSTOMERBaseService
 * @extends {EntityBaseService}
 */
export class CUSTOMERBaseService extends EntityBaseService<ICUSTOMER> {
    protected get keys(): string[] {
        return keys;
    }
    protected SYSTEMNAME = 'ActiveTYYW';
    protected APPNAME = 'App2';
    protected APPDENAME = 'CUSTOMER';
    protected APPWFDENAME = 'CUSTOMER';
    protected APPDENAMEPLURAL = 'CUSTOMERs';
    protected dynaModelFilePath:string = 'PSSYSAPPS/App2/PSAPPDATAENTITIES/CUSTOMER.json';
    protected APPDEKEY = 'customerid';
    protected APPDETEXT = 'customername';
    protected quickSearchFields = ['customername',];
    protected selectContextParam = {
    };

    constructor(opts?: any) {
        super(opts, 'CUSTOMER');
    }

    newEntity(data: ICUSTOMER): CUSTOMER {
        return new CUSTOMER(data);
    }

    /**
     * Create
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async Create(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        _data = await this.beforeExecuteAction(_context,_data,'Create');
        if (!_data.srffrontuf || _data.srffrontuf != 1) {
            _data[this.APPDEKEY] = null;
        }
        if (_data.srffrontuf != null) {
            delete _data.srffrontuf;
        }
    _data = await CUSTOMERDTOHelp.get(_context,_data);
        const res = await this.http.post(`/customers`, _data);
        res.data = await CUSTOMERDTOHelp.set(_context,res.data);
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * Get
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async Get(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        const res = await this.http.get(`/customers/${encodeURIComponent(_context.customer)}`, _data);
        res.data = await CUSTOMERDTOHelp.set(_context,res.data);
        res.data = await this.afterExecuteAction(_context,res?.data,'Get');
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * GetDraft
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async GetDraft(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        _data[this.APPDENAME?.toLowerCase()] = undefined;
        _data[this.APPDEKEY] = undefined;
        const res = await this.http.get(`/customers/getdraft`, _data);
        res.data = await CUSTOMERDTOHelp.set(_context,res.data);
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * Remove
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async Remove(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        const res = await this.http.delete(`/customers/${encodeURIComponent(_context.customer)}`);
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * Update
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async Update(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        _data = await this.beforeExecuteAction(_context,_data,'Update');
    _data = await CUSTOMERDTOHelp.get(_context,_data);
        const res = await this.http.put(`/customers/${encodeURIComponent(_context.customer)}`, _data);
        res.data = await CUSTOMERDTOHelp.set(_context,res.data);
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * FetchDefault
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async FetchDefault(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        const res = await this.http.get(`/customers/fetchdefault`, _data);
        res.data = await CUSTOMERDTOHelp.ToDataObjArray(_context,res.data);
        res.data = await this.afterExecuteActionBatch(_context,res?.data,'FetchDefault');
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
    /**
     * Select
     *
     * @param {*} [_context={}]
     * @param {*} [_data = {}]
     * @returns {Promise<HttpResponse>}
     * @memberof CUSTOMERService
     */
    async Select(_context: any = {}, _data: any = {}): Promise<HttpResponse> {
        try {
        const res = await this.http.get(`/customers/${encodeURIComponent(_context.customer)}/select`);
        return res;
            } catch (error) {
                return this.handleResponseError(error);
            }
    }
}
